package com.fz.us.oa.core.service.com.impl;

import com.fz.us.admin.base.bean.BaseEnumManager;
import com.fz.us.admin.base.bean.search.SearchEnum;
import com.fz.us.admin.base.bean.search.order.Order;
import com.fz.us.admin.base.bean.search.page.PageHelp;
import com.fz.us.admin.base.bean.search.page.PagePaging;
import com.fz.us.admin.base.bean.search.result.SearchResultPaging;
import com.fz.us.admin.base.bean.search.rule.Rule;
import com.fz.us.admin.base.bean.search.rule.RuleHelper;
import com.fz.us.admin.base.dao.BaseDao;
import com.fz.us.admin.base.service.impl.BaseServiceImpl;
import com.fz.us.admin.company.entity.User;
import com.fz.us.oa.core.dao.com.NoticeReadDao;
import com.fz.us.oa.core.entity.com.NoticeRead;
import com.fz.us.oa.core.service.com.NoticeReadService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by ZhuChunXiao on 2017/7/21.
 */
@Service
public class NoticeReadServiceImpl extends BaseServiceImpl<NoticeRead,String> implements NoticeReadService {
    @Resource
    NoticeReadDao noticeReadDao;

    @Override
    public BaseDao<NoticeRead, String> getBaseDao() {
        return noticeReadDao;
    }

    @Override
    public boolean hasReaded(User user, String noticeId) {
        ArrayList<Rule> rules = new ArrayList<Rule>();

        rules.add(RuleHelper.eq("creater", user));
        rules.add(RuleHelper.eq("state", BaseEnumManager.StateEnum.Enable));
        rules.add(RuleHelper.eq("notice.id", noticeId));

        SearchResultPaging searchResult = findPagingBySearch(PageHelp.page(1, PagePaging.MAX_PAGE_SIZE), rules, Order.order("createTime", SearchEnum.OrderType.desc));
        if (searchResult != null){
            if (searchResult.getList().size()>0){
                return true;
            }
        }
        return false;
    }

    @Override
    public List<NoticeRead> findUsersByNoticeRead(String userId, String noticeId) {
        return noticeReadDao.findUsersByNoticeRead(userId,noticeId);
    }
}
